//
//  MyInviteVC.m
//  BXlive
//
//  Created by bxlive on 2017/5/3.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "MyInviteVC.h"
#import "InvitaonRegCell.h"
#import <UMShare/UMShare.h>
#import <Photos/Photos.h>
#import <iCarousel.h>
#import "SLShareObjs.h"
#import "SLShareView.h"
#import <Aspects.h>
#import <UMShare/UMSocialManager.h>
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "GradientButton.h"
@interface MyInviteVC ()<iCarouselDataSource,iCarouselDelegate>

@property(nonatomic,strong)UIView *navigationView;

@property(nonatomic,strong)UIScrollView *contentScrollView;

@property(nonatomic, strong)iCarousel *myCarousel;

@property(nonatomic, strong)UILabel *invitationCodeL;

@property(nonatomic,strong)NSString *zbid;

@property (nonatomic ,strong) NSString * urlString;

@property (nonatomic, strong) NSString *invite_apply_url;

@property (nonatomic, strong)NSArray *allImageUrl;

@property (nonatomic, strong) UIImageView *pictureImgView;

@property (nonatomic, strong)NSMutableArray *fakeAllImgArray;

@property (nonatomic, strong)NSMutableArray *shareObjectArray;

@end

@implementation MyInviteVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self setupNavigationView];
    [self.view setBackgroundColor:sl_subBGColors];

    _fakeAllImgArray =  [NSMutableArray new];
    self.zbid  = @"0";
    [self initViewUpdate];
//    [self initView];
    [self getLocationData];
    [self loadData];
    
    _shareObjectArray = [NSMutableArray new];
    
//    保存
    SLShareObjs *obj0 = [[SLShareObjs alloc] init];
    obj0.iconName = @"invite_bc";
    obj0.name = NSLocalizedString(@"保存", nil);
    obj0.shareType = SLShareObjsTypeOfSave;
    
    [_shareObjectArray addObject:obj0];
    
    if ([[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession]==YES) {
        SLShareObjs *obj1 = [[SLShareObjs alloc] init];
        obj1.iconName = @"invite_wx";
        obj1.name = NSLocalizedString(@"微信", nil);
        obj1.shareType = SLShareObjsTypeOfWechatSession;
        [_shareObjectArray addObject:obj1];
        
        SLShareObjs *obj2 = [[SLShareObjs alloc] init];
        obj2.iconName = @"invite_pyq";
        obj2.name = NSLocalizedString(@"朋友圈", nil);
        obj2.shareType = SLShareObjsTypeOfWechatTimeLine;
        [_shareObjectArray addObject:obj2];
    }
    if ([[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ]==YES) {
        SLShareObjs *obj3 = [[SLShareObjs alloc] init];
        obj3.iconName = @"invite_qq";
        obj3.name = @"QQ";
        obj3.shareType = SLShareObjsTypeOfQQ;
        [_shareObjectArray addObject:obj3];
        
        SLShareObjs *obj4 = [[SLShareObjs alloc] init];
        obj4.iconName = @"invite_qqkj";
        obj4.name = NSLocalizedString(@"QQ空间", nil);
        obj4.shareType = SLShareObjsTypeOfQzone;
        [_shareObjectArray addObject:obj4];
    }
    
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:sl_subBGColors];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"邀请好友", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

- (void)getLocationData {
    NSString *key = [NSString stringWithFormat:@"kMyInvite_%@",[BXLiveUser currentBXLiveUser].user_id];
    NSDictionary *dic  = (NSDictionary *)[CacheHelper objectForKey:key];
    if (dic) {
        [self didGetDatWithDic:dic];
    }
}

- (void)didGetDatWithDic:(NSDictionary *)dic {
//    self.nameLabel.text = dic[@"data"][@"nickname"];
//    [self.iconImage zzl_setImageWithURLString:[NSURL URLWithString:dic[@"data"][@"avatar"]] placeholder:[UIImage imageNamed:@"placeplaceholder"]];
    self.urlString = dic[@"data"][@"url"];
    self.invitationCodeL.text = dic[@"data"][@"invite_code"];
    
    self.allImageUrl = dic[@"data"][@"allImageUrl"];
    
    [_fakeAllImgArray removeAllObjects];
    NSInteger realCount = self.allImageUrl.count;
    if (realCount > 1) {
        [_fakeAllImgArray addObject:self.allImageUrl[realCount-2]];
        [_fakeAllImgArray addObject:self.allImageUrl.lastObject];
        [_fakeAllImgArray addObjectsFromArray:self.allImageUrl];
        [_fakeAllImgArray addObject:self.allImageUrl.firstObject];
        [_fakeAllImgArray addObject:self.allImageUrl[1]];
        [_myCarousel reloadData];
        _myCarousel.scrollOffset = 2;
    }else  if(realCount == 1) {
        [_fakeAllImgArray addObject:self.allImageUrl[0]];
        [_fakeAllImgArray addObject:self.allImageUrl[0]];
        [_fakeAllImgArray addObject:self.allImageUrl[0]];
        [_fakeAllImgArray addObject:self.allImageUrl[0]];
        [_fakeAllImgArray addObject:self.allImageUrl[0]];
        [_myCarousel reloadData];
        _myCarousel.scrollOffset = 2;
    }else{
        return;
    }
//    self.invite_apply_url = dic[@"data"][@"invite_apply_url"];
    NSLog(@"%@",dic);
}

-(void)loadData{

    [[NewHttpRequestHuang sharedNewHttpRequestHuang]InviteGenerateWithAnchor:self.zbid Success:^(id responseObject) {
        if ([responseObject[@"code"] intValue]==0) {
            if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                [self didGetDatWithDic:responseObject];
                
                if (![self.zbid integerValue]) {
                    NSString *key = [NSString stringWithFormat:@"kMyInvite_%@",[BXLiveUser currentBXLiveUser].user_id];
                    [CacheHelper setObject:responseObject forKey:key];
                }
            }
        }
    } Failure:^(NSError *error) {
        
    }];
}

- (void)initViewUpdate {
    self.pictureImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"invite_c"]];
    [self.view addSubview:self.pictureImgView];
    [self.pictureImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_navigationView.mas_bottom);
        make.height.mas_equalTo(__ScaleWidth(300));
    }];
    
    
    UIButton *fzljBtn =[[GradientButton alloc ] initWithFrame:CGRectZero];
    fzljBtn.titleLabel.font = SLPFFont(16);
    [self.view addSubview:fzljBtn];
    [fzljBtn setTitle:@"复制邀请链接" forState:UIControlStateNormal];
    [fzljBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pictureImgView.mas_bottom).offset(__ScaleWidth(22.5));
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(__ScaleWidth(227));
        make.height.mas_equalTo(__ScaleWidth(48));
    }];
    fzljBtn.layer.cornerRadius = 24;
    fzljBtn.clipsToBounds = YES;
    [fzljBtn addTarget:self action:@selector(fzljAction) forControlEvents:BtnTouchUpInside];

    UILabel *noticeLabel = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:@"" Font:SLPFFont(12) TextColor:sl_subBGColors];
    [self.view addSubview:noticeLabel];
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(-__ScaleWidth(12));
        make.top.mas_equalTo(fzljBtn.mas_bottom).offset(__ScaleWidth(23));
    }];
    
    NSMutableAttributedString *noticeAttri = [[NSMutableAttributedString alloc] initWithString:@"* " attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#F26F8F"], NSFontAttributeName: SLPFFont(11)}];
    [noticeAttri appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"好友下载APP并使用你的邀请码成功注册后，TA将成为你的粉丝，粉丝下单，你也可以获得收益哦！", nil) attributes:@{NSForegroundColorAttributeName: sl_textSubColors,NSFontAttributeName: SLPFFont(11)}]];
    
    noticeLabel.attributedText = noticeAttri;
    noticeLabel.numberOfLines = 0;
}
- (void)initView{
    _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, __kTopAddHeight+64, __kWidth, __kHeight-__kTopAddHeight-64-__kBottomAddHeight)];
    _contentScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_contentScrollView];
//    WS(weakSelf);
    CGFloat contentSizeH = __ScaleWidth(35);
    _contentScrollView.scrollEnabled = YES;
    
    _myCarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, __ScaleWidth(35), __kWidth, __ScaleWidth(448))];
    _myCarousel.dataSource = self;
    _myCarousel.delegate = self;
    _myCarousel.bounces = NO;
    _myCarousel.pagingEnabled = YES;
    _myCarousel.type = iCarouselTypeCustom;
    
//    [_contentScrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
    
    [_contentScrollView addSubview:_myCarousel];
    
    contentSizeH = contentSizeH + __ScaleWidth(448+31);
    
 
//    邀请码
    UIView *invitationV = [UIView new];
    invitationV.hidden = YES;
    [_contentScrollView addSubview:invitationV];
    invitationV.backgroundColor = sl_BGColors;
    invitationV.layer.masksToBounds = YES;
    invitationV.layer.cornerRadius = __ScaleWidth(43/2.0);
    [invitationV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(__ScaleWidth(313));
        make.height.mas_equalTo(__ScaleWidth(43));
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(contentSizeH);
    }];
    UILabel *yqmTitleLabel = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"邀请码", nil) Font:SLBFont(__ScaleWidth(16)) TextColor:sl_textSubColors];
    [invitationV addSubview:yqmTitleLabel];
    [yqmTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(15));
        make.width.mas_equalTo(__ScaleWidth(90));
        make.height.mas_equalTo(__ScaleWidth(22));
        make.centerY.mas_equalTo(0);
    }];
    
    UIButton *copyBtn = [[GradientButton alloc]  initWithFrame:CGRectZero];
    [invitationV addSubview:copyBtn];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-__ScaleWidth(15));
        make.width.mas_equalTo(__ScaleWidth(66));
        make.height.mas_equalTo(__ScaleWidth(27));
        make.centerY.mas_equalTo(0);
    }];
    [copyBtn setTitle:NSLocalizedString(@"复制", nil) forState:BtnNormal];
//    [copyBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
//    [copyBtn setBackgroundImage:CImage(@"wallat_sure") forState:UIControlStateNormal];
    copyBtn.titleLabel.font = SLBFont(14);
//    copyBtn.backgroundColor = sl_normalColors;
    copyBtn.layer.masksToBounds = YES;
    copyBtn.layer.cornerRadius = __ScaleWidth(13.5);
    [copyBtn addTarget:self action:@selector(copyBtnOnClick) forControlEvents:BtnTouchUpInside];
    
    _invitationCodeL = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:@"2312331" Font:SLBFont(__ScaleWidth(16)) TextColor:sl_textColors];
    [invitationV addSubview:_invitationCodeL];
    [_invitationCodeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yqmTitleLabel.mas_right).offset(__ScaleWidth(15));
        make.height.mas_equalTo(__ScaleWidth(22));
        make.centerY.mas_equalTo(0);
        make.right.equalTo(copyBtn.mas_left).offset(-__ScaleWidth(10));
    }];
    
    contentSizeH = contentSizeH + __ScaleWidth(20 + 15 + 43);
    
//    分享
    UIView *shareV =  [UIView new];
    [_contentScrollView addSubview:shareV];
    shareV.layer.masksToBounds = YES;
    shareV.layer.cornerRadius = 12;
    shareV.backgroundColor = sl_BGColors;
    
    [shareV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(__ScaleWidth(313));
        make.height.mas_equalTo(__ScaleWidth(102));
        make.centerX.mas_equalTo(0);
        make.top.equalTo(invitationV.mas_bottom).offset(__ScaleWidth(20));
    }];
    
//    复制链接
    UIButton *fzljBtn =[[GradientButton alloc ] initWithFrame:CGRectZero];
    [shareV addSubview:fzljBtn];
//    [fzljBtn setBackgroundImage:CImage(@"wallat_sure") forState:UIControlStateNormal];
//    [fzljBtn setImage:CImage(NSLocalizedString(@"copy_linkurl", nil)) forState:BtnNormal];
//    [fzljBtn setImage:CImage(NSLocalizedString(@"copy_linkurl", nil)) forState:UIControlStateHighlighted];
    [fzljBtn setTitle:@"复制链接邀请" forState:UIControlStateNormal];
    [fzljBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(__ScaleWidth(50));
        make.right.mas_equalTo(__ScaleWidth(-50));
        make.height.mas_equalTo(__ScaleWidth(48));
    }];
    fzljBtn.layer.cornerRadius = 24;
    fzljBtn.clipsToBounds = YES;
    [fzljBtn addTarget:self action:@selector(fzljAction) forControlEvents:BtnTouchUpInside];
    
//    分享海报
//    UIButton *fxhbBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    [shareV addSubview:fxhbBtn];
//    [fxhbBtn setImage:CImage(NSLocalizedString(@"share_posters", nil)) forState:BtnNormal];
//    [fxhbBtn setImage:CImage(NSLocalizedString(@"share_posters", nil)) forState:UIControlStateHighlighted];
//
//    [fxhbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(__ScaleWidth(-26));
//        make.top.mas_equalTo(__ScaleWidth(15));
//        make.width.mas_equalTo(__ScaleWidth(104));
//        make.height.mas_equalTo(__ScaleWidth(32));
//    }];
//
//    [fxhbBtn addTarget:self action:@selector(fxhbAction) forControlEvents:BtnTouchUpInside];
    
    
    UILabel *noticeLabel = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:@"" Font:SLPFFont(12) TextColor:sl_subBGColors];
    [shareV addSubview:noticeLabel];
    CGFloat noticeH = [UILabel getHeightByWidth:__ScaleWidth(187) title:NSLocalizedString(@"* 好友下载APP并使用你的邀请码成功注册后，TA将成为你的粉丝，粉丝下单，你也可以获得收益哦！", nil) font:SLPFFont(12)];
    [shareV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(__ScaleWidth(55+15) + noticeH);
    }];
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(13));
        make.right.mas_equalTo(-__ScaleWidth(13));
        make.height.mas_equalTo(noticeH);
        make.top.mas_equalTo(__ScaleWidth(55));
    }];
    
    NSMutableAttributedString *noticeAttri = [[NSMutableAttributedString alloc] initWithString:@"* " attributes:@{NSForegroundColorAttributeName: sl_normalColors, NSFontAttributeName: SLPFFont(11)}];
    [noticeAttri appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"好友下载APP并使用你的邀请码成功注册后，TA将成为你的粉丝，粉丝下单，你也可以获得收益哦！", nil) attributes:@{NSForegroundColorAttributeName: sl_textSubColors,NSFontAttributeName: SLPFFont(11)}]];
    
    noticeLabel.attributedText = noticeAttri;
    noticeLabel.numberOfLines = 0;
    
    contentSizeH = contentSizeH + __ScaleWidth(55+15) + noticeH;
    
    [_contentScrollView setContentSize:CGSizeMake(self.view.width, contentSizeH)];
}

#pragma mark - 复制链接
-(void)fzljAction{
    [[UIPasteboard generalPasteboard] setString:self.urlString];
    [SLProgressHUD slShowInfoWithMessage:NSLocalizedString(@"链接已复制", nil)];
}

#pragma mark - 分享海报
-(void)fxhbAction{
//    弹出分享框
    SLShareView *shareV = [[SLShareView alloc] initWithShareObjects:self.shareObjectArray];
    WS(weakSelf);
    shareV.shareTo = ^(SLShareObjsType type) {
        
        if (type == SLShareObjsTypeOfSave) {
//                保存
            [weakSelf saveBtnClick];
        }else if( type == SLShareObjsTypeOfWechatSession){
            
            [weakSelf sharePicWithType:type];
        }else if( type == SLShareObjsTypeOfWechatTimeLine){
            [weakSelf sharePicWithType:type];
        }else if( type == SLShareObjsTypeOfQQ){
            [weakSelf sharePicWithType:type];
        }else if( type == SLShareObjsTypeOfQzone){
            [weakSelf sharePicWithType:type];
        }
    };
    [shareV show];
}

-(void)sharePicWithType:(SLShareObjsType)type{
    WS(weakSelf);
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusAuthorized) {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:weakSelf.fakeAllImgArray[weakSelf.myCarousel.currentItemIndex]]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIImage *image = [UIImage imageWithData:data]; // 取得图片
                        //创建分享消息对象
                        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

                        //创建图片内容对象
                        UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
                        //如果有缩略图，则设置缩略图
                        shareObject.thumbImage = [UIImage imageNamed:@"icon"];
                        [shareObject setShareImage:image];

                        //分享消息对象设置分享内容对象
                        messageObject.shareObject = shareObject;

                        //调用分享接口
                        [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
                            if (error) {
                                NSLog(@"************Share fail with error %@*********",error);
                            }else{
                                NSLog(@"response data is %@",data);
                            }
                        }];
                    });
                });
            } else {
                [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"无相册权限，请在设置中打开", nil)];
            }
        });
    }];
}


#pragma mark - 复制邀请码
-(void)copyBtnOnClick{
    [[UIPasteboard generalPasteboard] setString:_invitationCodeL.text];
    [SLProgressHUD slShowInfoWithMessage:NSLocalizedString(@"邀请码已复制", nil)];
}

#pragma mark - 保存到相册
- (void)saveBtnClick {
    WS(weakSelf);
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusAuthorized) {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:weakSelf.fakeAllImgArray[weakSelf.myCarousel.currentItemIndex]]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIImage *image = [UIImage imageWithData:data]; // 取得图片
                        [self loadImageFinished:image];
                    });
                });
            } else {
                [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"无相册权限，请在设置中打开", nil)];
            }
        });
    }];
}

#pragma mark - iCarouselDataSource
 
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return self.fakeAllImgArray.count;
}
 
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    if (view == nil) {
        CGFloat viewWidth = __ScaleWidth(252);
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, __ScaleWidth(448))];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 12;
    }
    [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:self.fakeAllImgArray[index]] placeholderImage:nil];
    
    return view;
}
 
#pragma mark - iCarouselDelegate
 
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform {
    
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 354.0f/448.0f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    
    return CATransform3DTranslate(transform, offset * self.myCarousel.itemWidth * (1+(1-354.0f/448.0f)), 0.0, 0.0);
}
 
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"%@",[NSString stringWithFormat:NSLocalizedString(@"点击了第%ld张图片", nil),(long)index]);
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel;{
    if (carousel.currentItemIndex == 1) {
        [carousel scrollToItemAtIndex:self.fakeAllImgArray.count-3 animated:NO];
    }else if (carousel.currentItemIndex == self.fakeAllImgArray.count-2) {
        [carousel scrollToItemAtIndex:2 animated:NO];
    }
}

- (void)loadImageFinished:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

#pragma mark -- <保存到相册>
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if(error){
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"保存图片失败", nil)];
    } else {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"保存图片成功", nil)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
